package com.abe.mvp.view.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.app.RelayoutTool;
import com.abe.mvp.model.bean.MainExerciseBean;
import com.abe.mvp.view.activity.MainBallOldOldActivity;
import com.abe.mvp.view.activity.MainProgressActivity;
import com.abe.strengthtrainer.R;

@SuppressLint({ "InflateParams", "ClickableViewAccessibility" })
public class MainExerciseInfoAdapter extends BaseAdapter {
	private Context context;
	private List<MainExerciseBean> data;
	private LayoutInflater layoutInflater;
	private float scale;

	public MainExerciseInfoAdapter(Context context,
			List<MainExerciseBean> data, float scale) {
		this.data = data;
		this.scale = scale;
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public final class ViewHolder {
		public LinearLayout layout;
		public TextView name, count;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	/**
	 * 获得某一位置的数
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	/**
	 * 获得唯一标识
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	private View initBallExerciseView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(
					R.layout.item_grid_main_exercise_info, null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			viewHolder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_layout);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.item_name);
			viewHolder.count = (TextView) convertView
					.findViewById(R.id.item_count);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		MainExerciseBean bean = data.get(position);
		viewHolder.name.setText(bean.getName());
		viewHolder.count.setText(bean.getData());
		return convertView;
	}

	private View initProgressExerciseView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(
					R.layout.item_grid_main_exercise_info_new, null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			viewHolder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_layout);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.item_name);
			viewHolder.count = (TextView) convertView
					.findViewById(R.id.item_count);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		MainExerciseBean bean = data.get(position);
		viewHolder.name.setText(bean.getName());
		viewHolder.count.setText(bean.getData());
		return convertView;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (context.getClass().equals(MainBallOldOldActivity.class)) {
			convertView = initBallExerciseView(position, convertView, parent);
		} else if (context.getClass().equals(MainProgressActivity.class)) {
			convertView = initProgressExerciseView(position, convertView,
					parent);
		}
		return convertView;
	}

}
